{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# * args **kwargs 参数传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cat( a,b=\"b\",*args ,**kwargs):\n",
    "    print(\"a\",a)\n",
    "    print(\"b\",b)\n",
    "    print(\"args\",args)\n",
    "   # for i in kwargs\n",
    "    print(\"kwargs\",kwargs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# kwargs 可以使用key { }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "()\n",
      "{'gg': 'gg', 'ff': ''}\n"
     ]
    }
   ],
   "source": [
    "cat(\"a\",ff=\"\",gg=\"gg\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', {'aaa': 2})\n",
      "('b', {'bbb': 33})\n",
      "('args', (set([44, 'ccc']), set(['ddd', 55])))\n",
      "('kwargs', {})\n"
     ]
    }
   ],
   "source": [
    "cat({\"aaa\":2},{\"bbb\":33},{\"ccc\",44},{\"ddd\",55})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('kwargs', {'a': {'ddd': 222}})\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "nan\n"
     ]
    }
   ],
   "source": [
    "print(math.isnan(float(\"nan\")))\n",
    "print( float(\"nan\") is float(\"inf\") )\n",
    "print( str( float(\"nan\")))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# kwargs "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': {'ddd': 222}}\n",
      "{'a': {'ddd': 222}}\n"
     ]
    }
   ],
   "source": [
    "def dog(**kwargs):\n",
    "    print(kwargs)\n",
    "    #print(**kwargs) err\n",
    "dog(a={\"ddd\":222})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3, 2)\n",
      "({'q': 2, 'e': 2, 'w': 3}, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "def b(q=1,w=2,e=3):\n",
    "    print(q,w,e)\n",
    "    \n",
    "b(**{\"q\":2,\"w\":3,\"e\":2})\n",
    "b({\"q\":2,\"w\":3,\"e\":2})\n",
    "#若 **中的字典不能与函数参数对应 会报错b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'q': 2, 'e': 2, 'w': 3}\n",
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "def c(**kwargs):\n",
    "    print(kwargs)\n",
    "    print(kwargs[\"q\"])\n",
    "    def d(q=3,e=3,w=3):\n",
    "        print(q)\n",
    "    d(**kwargs)\n",
    "c(**{\"q\":2,\"w\":3,\"e\":2})\n",
    "\n",
    "# ** 表示拆开成 x=y ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 拆解过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(([3, 5, 6, 3],), <type 'tuple'>)\n",
      "([3, 5, 6, 3],)\n"
     ]
    }
   ],
   "source": [
    "t=(1,3,4,5)\n",
    "l=[3,5,6,3]\n",
    "\n",
    "def cat(*args):\n",
    "    print(args,type(args))\n",
    "    print(args)\n",
    "cat(l)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
